package com.ry.est.controller;

import com.ry.est.common.controller.BaseController;
import com.ry.est.dto.CommonResult;
import com.ry.est.dto.EvaluateDTO;
import com.ry.est.dto.EvaluateInfoDTO;
import com.ry.est.dto.MalfunctionDTO;
import com.ry.est.entity.Users;
import com.ry.est.service.EvaluateService;
import com.ry.est.service.MalfunctionService;
import com.ry.est.service.TokenService;
import com.ry.est.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品维修估价控制器
 * @author ry
 * @since 2021-11-09 09:05
 **/
@Validated
@RestController
@Api(tags = "商品维修估价api")
public class EvaluateController extends BaseController {
    @Autowired
    private MalfunctionService malfunctionService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private EvaluateService evaluateService;

    @ApiOperation("根据商品id获取评估维度信息")
    @GetMapping("/api/evaluate/specList")
    public CommonResult<List<MalfunctionDTO>> getSpecList(
            @ApiParam(value = "商品id",required = true)
            @RequestParam @Range(min = 1,message = "无效的商品id值") Long modelId){
        List<MalfunctionDTO> malfunctionDTOS = malfunctionService.listByModelId(modelId);

        return ResultUtil.success(malfunctionDTOS);
    }

    @ApiOperation("根据用户选择的故障信息进行估价")
    @PostMapping("/api/evaluate/assess")
    public CommonResult<EvaluateDTO> assess(@ApiParam("被评估的商品") Long modelId,
                                            @RequestParam(value = "optionIds") List<Long> optionIds){
        String token = request.getHeader("token");
        Users users = tokenService.getUserInfoByToken(token);
        EvaluateDTO evaluateDTO = evaluateService.assess(modelId, optionIds, users);

        return ResultUtil.success(evaluateDTO);
    }

    @ApiOperation("根据id获取评估的详细信息")
    @GetMapping("/api/evaluate/{id}/assessInfo")
    public CommonResult<EvaluateInfoDTO> assess(@PathVariable("id")Long id){
        EvaluateInfoDTO evaluateInfoDTO = evaluateService.getEvaluateInfoById(id);

        return ResultUtil.success(evaluateInfoDTO);
    }
}
